#!/bin/bash
if [ "$#" -ne 1 ]; then
    echo -e "\e[31mERROR :  The url to the youtube video is required"
    exit 1
fi
i=0
downloadtoken=0
IFS_BAK=$IFS
IFS=$'\n'
tarray=$(youtube-dl -F $1 |grep "audio only" | cut -c1-3,12-18,45-50)
for line in $tarray; do
 i=$((i+1))
 if [ $(( $i % 2 )) -eq 0 ]; then
 	echo -e "\e[0m$i. $line"
 else
 	echo -e "\e[104m$i. $line"
 fi
 downloadtoken=$(echo -e $line | cut -c1-3)
 	#reset
done 

option=${i}
echo  -e "\e[0mSelect which version to download ($option):"
read option
i=0
if [[ $option ]]; then
	for line in $tarray; do
	 i=$((i+1))
	 if [ $(( $i)) -eq $option ]; then
	 	downloadtoken=$(echo -e $line | cut -c1-3)
	 fi
	done 
fi

echo -e "Downloading selected version : \e[104m$downloadtoken\e[0m" 
youtube-dl -f $downloadtoken $1